import 'dart:convert';

import 'package:appflutterhomeschool/api/api_service.dart';
import 'package:appflutterhomeschool/api/dio_utils.dart';
import 'package:appflutterhomeschool/model/tab_model.dart';
import 'package:flutter/material.dart';

///用户： ninetailedfox
///时间： 2020/7/24
///邮箱： 1037438704@qq.com
///功能： 表单详情状态管理

class FormDetailsViewModel extends ChangeNotifier {
  BuildContext me;
  int notificationId;

  String title = "";
  String datetime = "";
  int shouldBeFilledIn = 0;
  int readString = 0;

  Map<String, dynamic> mapJson;

  List<TabModel> listTab = [
    TabModel("数据统计", true),
    TabModel("详细数据", false),
  ];

  FormDetailsViewModel(this.me, this.notificationId);

  initView() {
    Dio2Utils().post(ApiService.teacherGetNotificationDetail, {
      "notification_id": notificationId //类型：String  必有字段  备注：通知id
    }, (successCallBack) {
      Map data = successCallBack["data"];
      if (data == null) {
        return;
      }
      mapJson = data["notification_data"];
      if (mapJson == null) {
        return;
      }
      title = mapJson["title"];
      String infoString = mapJson["info"];
      Map infoJosn = jsonDecode(infoString);
      if (infoJosn["write"] != null) {
        List writeList = infoJosn["write"];
        shouldBeFilledIn = writeList.length;
      }
      if (infoJosn["read"] != null) {
        List readList = infoJosn["read"];
        readString = readList.length;
      }
      DateTime datatime = DateTime.parse(mapJson["datetime"]);
      datetime = "发布于${datatime.month}-${datatime.day}\t ${datatime.hour}:${datatime.minute}";
      notifyListeners();
    }, (errorCallBack) {});
  }

  tapModelOnTap(int index) {
    for (int i = 0; i < listTab.length; i++) {
      listTab[i].display = false;
    }
    listTab[index].display = true;
    notifyListeners();
  }
}
